import {URI_QUANSHOU_POST_CODE, URI_PARTWORKSTATE} from "../../../utils/api";
import {ajax} from "../../../utils/httpRequest";

Page({

  /**
   * 页面的初始数据
   */
  data: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  bindGetUserInfo: function (e) {
    const that = this;
    const encryptedData = e.detail.encryptedData;
    const faceUrl = e.detail.userInfo.avatarUrl;//console.log(e.detail.userInfo.avatarUrl);
    const iv = e.detail.iv;
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      wx.login({
        success: (res) => {
          if (res.code) {
            const appid = 'wx4e13b119a560027e';
            const appsecret = '01167d4836a8c74974139494fc1d7535';
            ajax.get(URI_QUANSHOU_POST_CODE, {
              code: res.code,
              appid: appid,
              appsecret: appsecret,
              iv: iv,
              encryptedData: encryptedData
            }).then(res => {
              wx.setStorageSync('only_u', res.uuuuuu);
              wx.setStorageSync('faceUrl', faceUrl);
              if (res.info) {
                wx.setStorageSync('uid', res.info.id);
                wx.setStorageSync('token', res.info.token);
                wx.setStorageSync('tel', res.info.tel);
                this.getApplyState();
              } else {
                wx.reLaunch({
                  url: '/pages/entry/regist/regist',
                });
                return false;
              }
            }).catch(error => {
              console.log(error)
            })
          }
        },
        fail() {
          wx.showToast({
            title: '异常，请重试',
            icon: 'none',
            duration: 3000
          })
        }
      })

    } else {
      //用户按了拒绝按钮
      wx.showToast({
        title: '只有授权才能登陆...',
        icon: 'none',
        duration: 2000
      })
    }
  },

  getApplyState() {
    ajax.post(URI_PARTWORKSTATE, {
      uid: wx.getStorageSync('uid')
    }).then(res => {
      if (res.state == 0) {
        wx.showModal({
          title: '温馨提示',
          content: '您还没有录入党员信息,是否要去录入？',
          confirmColor: '#e83329',
          confirmText: '是',
          cancelText: '否',
          success: (res) => {
            if (res.confirm) {
              wx.navigateToMiniProgram({
                appId: 'wxea598e0f5823e621',
                path: 'pages/entry/authorize/authorize',
                //path: 'pages/index/index',
                extraData: {},
                envVersion: 'release',
                success(res) {
                  // 打开成功
                  console.log(res);
                },
                fail(error) {
                  console.log(error);
                }
              })
            }
          }
        })
      } else if (res.state == 1) {
        wx.reLaunch({
          url: '/pages/index/index',
        })
      } else if (res.state == 2) {
        wx.showModal({
          title: '温馨提示',
          content: '党员信息正在审核中',
          showCancel: false,
          confirmColor: '#e83329',
          success:(res)=>{
            if(res.confirm){

            }
          }
        })
      } else if (res.state == 3) {
        wx.showModal({
          title: '温馨提示',
          content: '审核不通过需要重新录入',
          confirmColor: '#e83329',
          success: (res) => {
            if (res.confirm) {
              wx.navigateToMiniProgram({
                appId: 'wxea598e0f5823e621',
                path: 'pages/entry/authorize/authorize',
                //path: 'pages/index/index',
                extraData: {},
                envVersion: 'release',
                success(res) {
                  // 打开成功
                  console.log(res);
                },
                fail(error) {
                  console.log(error);
                }
              })
            }
          }
        })
      }
    })
  },
  onShareAppMessage(){
    return {
      title:'党内工作',
      path:'pages/entry/authorize/authorize',
      success:()=>{
        console.log('转发成功');
      },
      fail:()=>{
        console.log('转发失败');
      }
    }
  }
});